//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;

namespace STT.Business
{
    public class BTbDanToc
    {
        public static DataTable Search(ETbDanToc dt)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDT", dt.TenDT);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDanToc_Search", pr);
            return all;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu_Sua(ETbDanToc dt)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenDT", dt.TenDT);
            pr[1] = new SqlParameter(@"MaDT", dt.MaDT);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDanToc_KiemTraDuLieu_Sua", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu(ETbDanToc dt)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDT", dt.TenDT);
     
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDanToc_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDanToc_SelectAll", null);
            return all;
        }
        //-----------------------------------------------------------//
        public static DataTable SelectAll1()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbDanToc_SelectAll1", null);
            return all;
        }
        //-----------------------------------------------------------//
        public static void Insert(ETbDanToc OTbDanToc)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenDT", OTbDanToc.TenDT);
            pr[1] = new SqlParameter(@"TrangThai", OTbDanToc.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDanToc_Insert", pr);
        }
        //-----------------------------------------------------------//
        public static void Update(ETbDanToc OTbDanToc)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaDT", OTbDanToc.MaDT);
            pr[1] = new SqlParameter(@"TenDT", OTbDanToc.TenDT);
            pr[2] = new SqlParameter(@"TrangThai", OTbDanToc.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDanToc_Update", pr);
        }
        //-----------------------------------------------------------//
        public static void Delete(int MaDT)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDT", MaDT);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDanToc_Delete", pr);
        }
        //-----------------------------------------------------------//
        public static ETbDanToc SelectByID(int MaDT)
        {
            ETbDanToc OTbDanToc = new ETbDanToc();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaDT", MaDT);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDanToc_SelectByID", pr);
            if (idr.Read())
                OTbDanToc = GetOneTbDanToc(idr);
            idr.Close();
            idr.Dispose();
            return OTbDanToc;
        }
        //-----------------------------------------------------------//
        private static ETbDanToc GetOneTbDanToc(IDataReader idr)
        {
            ETbDanToc OTbDanToc = new ETbDanToc();
            if (idr["MaDT"] != DBNull.Value)
                OTbDanToc.MaDT = (int)idr["MaDT"];
            if (idr["TenDT"] != DBNull.Value)
                OTbDanToc.TenDT = (string)idr["TenDT"];
            if (idr["TrangThai"] != DBNull.Value)
                OTbDanToc.TrangThai = (int)idr["TrangThai"];
            return OTbDanToc;
        }
        //-----------------------------------------------------------//
        public static bool TestByID(int MaDT)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaDT", MaDT);
            pr[1] = new SqlParameter(@"TestID", SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbDanToc_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
        //-----------------------------------------------------------//
        public static List<ETbDanToc> ListAll()
        {
            List<ETbDanToc> list = new List<ETbDanToc>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbDanToc_SelectAll", null);
            while (idr.Read())
                list.Add(GetOneTbDanToc(idr));
            if (idr.IsClosed == false)
            {
                idr.Close();
                idr.Dispose();
            }
            return list;
        }
    }

}
